package com.dtflys.forest.example.amap.forest;

import com.dtflys.forest.logging.DefaultLogHandler;
import com.dtflys.forest.logging.ResponseLogMessage;
import com.dtflys.forest.utils.StringUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author gongjun[dt_flys@hotmail.com]
 * @since 2021-10-20 1:09
 */
public class AmapResultLogHandler extends DefaultLogHandler {

    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void logResponseContent(ResponseLogMessage responseLogMessage) {
        if (responseLogMessage.getResponse() != null) {
            String content = responseLogMessage.getResponse().getContent();
            if (StringUtils.isNotEmpty(content)) {
                try {
                    JsonNode node = objectMapper.readTree(content);
                    content = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(node);
                    logContent("Response Content:\n" + content);
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }

            }
        }

    }
}
